﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.Net;
using System.Text;
using System.Configuration;
using MiniBug.Mail;
using MiniBug;

namespace MiniBug.Mail
{
    /// <summary>
    /// 
    /// </summary>
    public class MailSender:IDisposable
    {
        private MailSenderConfiguration configuration;

        private NetworkCredential authentication = null;
        private SmtpClient smtp = null;

        public bool EnabledSsl { get { return configuration.SmtpEnabledSSL; } }

        /// <summary>
        /// 
        /// </summary>
        public string SmtpServer { get { return configuration.SmtpServer; } }

        /// <summary>
        /// 
        /// </summary>
        public string MailAddressFrom { get { return configuration.MailAdrressFrom; } }

        /// <summary>
        /// 
        /// </summary>
        public string SmtpUserName { get { return configuration.SmtpUser; } }

        public int SmtpPort { get { return configuration.SmtpPort; } }
        /// <summary>
        /// 
        /// </summary>
        public MailSender() : this(new MailSenderConfiguration()) { }

        public MailSender(MailSenderConfiguration mailConfig)
        {
            configuration = mailConfig;
            authentication = new NetworkCredential(configuration.SmtpUser, configuration.SmtpPassword);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="to"></param>
        /// <param name="message"></param>
        public void Send(string to, string message)
        {
            this.Send(to, message, string.Empty);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="to"></param>
        /// <param name="message"></param>
        /// <param name="subject"></param>
        public void Send(string to, string message, string subject)
        {
            this.Send(to, message, subject, MailPriority.Normal);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="to"></param>
        /// <param name="message"></param>
        /// <param name="subject"></param>
        /// <param name="priority"></param>
        public void Send(string to, string message, string subject, MailPriority priority)
        {
            if (string.IsNullOrEmpty(to) || message.Equals(null) || subject.Equals(null))
            {
                throw new ArgumentException("the parameter to should not be empty or message/subject should not be null");
            }
            try
            {
                using (MailMessage mailMsg = new MailMessage(this.MailAddressFrom, to, subject, message))
                {
                    mailMsg.BodyEncoding = Encoding.UTF8;
                    mailMsg.IsBodyHtml = true;
                    mailMsg.SubjectEncoding = Encoding.UTF8;
                    SmtpClient smtp = new SmtpClient();
                    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                    smtp.Host = this.SmtpServer;
                    smtp.EnableSsl = this.EnabledSsl;
                    smtp.Port = this.SmtpPort;
                    smtp.UseDefaultCredentials = false;
                    smtp.Credentials = authentication;
                    smtp.Send(mailMsg);
                }
            }
            catch (Exception)
            {
                throw;
            }            
        }
        private void Dispose(bool disposing)
        {           
            if (disposing)
            {
                GC.SuppressFinalize(this);
            }
        } 


        public void Dispose()
        {
            if (smtp != null)
            {
                smtp.Dispose();
            }
            Dispose(true);          
        }
    }
}